package com.bluesky.controller;

import com.alibaba.fastjson.JSON;
import com.bluesky.dto.lesson003.RequestBean;
import com.bluesky.dto.lesson003.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


/**
 * <b>description</b>： Java高并发、微服务、性能优化实战案例100讲，视频号：程序员路人，源码 & 文档 & 技术支持，请加个人微信号：itsoku <br>
 * <b>time</b>：2024/3/27 21:28 <br>
 * <b>author</b>：ready likun_557@163.com
 */
@RestController
@Slf4j
@RequestMapping("/lesson003")
public class TestController {
    /**
     * 接口中没有任何处理代码，直接返回结果
     *
     * @return
     */
    @GetMapping("/test1")
    public String test1() {
        log.info("test1");
        return "ok";
    }

    /**
     * 接口中休眠100毫秒，用来模拟业务操作
     *
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/test2")
    public String test2() throws InterruptedException {
        //接口中休眠100毫秒，用来模拟业务操作
        TimeUnit.MILLISECONDS.sleep(100);
        return "ok";
    }

    /**
     * 带参的get请求(restful风格)
     * @return
     */
    @RequestMapping(value = "testGetByRestFul/{id}/{name}", method = RequestMethod.GET)
    public ResponseBean testGetByRestFul(@PathVariable(value = "id") String id, @PathVariable(value = "name") String name){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testGetByRestFul，请求参数id：" +  id + "请求参数name：" + name);
        return result;
    }

    /**
     * 带参的get请求(使用占位符号传参)
     * @return
     */
    @RequestMapping(value = "testGetByParam", method = RequestMethod.GET)
    public ResponseBean testGetByParam(@RequestParam("userName") String userName,
                                       @RequestParam("userPwd") String userPwd){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testGetByParam，请求参数userName：" +  userName + ",userPwd：" + userPwd);
        return result;
    }

    /**
     * 模拟表单请求，post方法测试
     * @return
     */
    @RequestMapping(value = "testPostByForm", method = RequestMethod.POST)
    public ResponseBean testPostByForm(@RequestParam("userName") String userName,
                                       @RequestParam("userPwd") String userPwd){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testPostByForm，请求参数userName：" + userName + ",userPwd:" + userPwd);
        return result;
    }

    /**
     * 模拟表单请求，post方法测试
     * @param request
     * @return
     */
    @RequestMapping(value = "testPostByFormAndObj", method = RequestMethod.POST)
    public ResponseBean testPostByForm(RequestBean request){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testPostByFormAndObj，请求参数：" + JSON.toJSONString(request));
        return result;
    }

    /**
     * 模拟JSON请求，post方法测试
     * @param request
     * @return
     */
    @RequestMapping(value = "testPostByJson", method = RequestMethod.POST)
    public ResponseBean testPostByJson(@RequestBody RequestBean request){
        ResponseBean result = new ResponseBean();
        result.setCode("200");
        result.setMsg("请求成功，方法：testPostByJson，请求参数：" + JSON.toJSONString(request));
        return result;
    }

    /**
     * 重定向
     * @param request
     * @return
     */
    @RequestMapping(value = "testPostByLocation", method = RequestMethod.POST)
    public String testPostByLocation(@RequestBody RequestBean request){
        return "redirect:index.html";
    }

    /**
     * 模拟JSON请求，put方法测试
     * @param request
     * @return
     */
    @RequestMapping(value = "testPutByJson", method = RequestMethod.PUT)
    public void testPutByJson(@RequestBody RequestBean request){
        System.out.println("请求成功，方法：testPutByJson，请求参数：" + JSON.toJSONString(request));
    }

    /**
     * 模拟JSON请求，delete方法测试
     * @return
     */
    @RequestMapping(value = "testDeleteByJson", method = RequestMethod.DELETE)
    public void testDeleteByJson(){
        System.out.println("请求成功，方法：testDeleteByJson");
    }


}
